<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use App\Models\Services;
use App\Models\Users;
use App\Models\Memberorders;
use App\Models\Usersinvitations;
use App\Models\Userswithdrawals;
use App\Models\Linkslistsimages;
use App\Models\Settingauthtxt;
use App\Models\Settingminiappsnotice;
use App\Models\Linkslistsminapps;
use App\Services\UploadService;
use Auth,Log,Exception;
use App\Jobs\Miniappswitch;
use GuzzleHttp\Client;

class SettingController extends Controller
{
    public function index(Request $request)
    {
        // 当前产品的产品码
        $code = "ttwl";
        // 产品分类:1.引流获客工具,2.企业运营工具,3.客服系统,4.二维码系统
        $sort_id = "1,3,4";
        $timestamp = time();
        $sign = md5($timestamp.$sort_id."moreqifu");

        $client = new Client();
        $response = $client->request("GET", "https://www.moreqifu.com/api/product_lists?sort_id=".$sort_id."&code=".$code."&timestamp=".$timestamp."&sign=".$sign);
        $results = json_decode($response->getBody());
        $productLists = $results->data->productLists;
        return view('admin.index.index',['productLists'=>$productLists,"code"=>$code]);
    }

    public function settingAuthTxt(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/app_articles")->with("status","error")->with("msg","您无权修改链接！");
        return view('admin.index.uploadtext');
    }

    public function settingAuthTxtSubmit(Request $request)
    {
        if (empty($request->textname)) return back()->with("status","error")->with("msg","请输入文件名称！");
        if (empty($request->text)) return back()->with("status","error")->with("msg","请输入文件内容！");
        $name = str_replace(".txt", "", $request->textname);
        $text = str_replace(" ", "", $request->text);
        Settingauthtxt::insertSettingAuthTxt($name,$text);
        return redirect('/admin/setting/setting_auth_txt')->with("status","success")->with("msg","上传成功,得到链接为: ".$request->textname);
    }

    public function settingMiniappsNotice(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权查看本页面！");
        $miniappsnotices = Settingminiappsnotice::getSettingMiniappsNotice();
        return view('admin.index.setting_miniapps_notice',['miniappsnotices'=>$miniappsnotices]);
    }

    public function linksListsPublic(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权修改链接！");
        return view('admin.index.links_lists_public');
    }

    public function linksListsPublicUpdate(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权修改链接！")->withInput();
        $LinkMinapp = Linkslistsminapps::getLinksListsMinappsByAppid($request->appid);
        if (empty($LinkMinapp)) return back()->with("status","error")->with("msg","要切换到的小程序不存在!")->withInput();
        if (empty($request->oldappid)) return back()->with("status","error")->with("msg","请输入旧的APPID")->withInput();
        // $miniappDisable = Redis::get("miniapp_change_".$request->oldappid);
        // if (!empty($miniappDisable)) return back()->with("status","error")->with("msg","旧的的小程序正在切换中,请等待处理完成再操作!")->withInput();
        if (empty($request->appid)) return back()->with("status","error")->with("msg","请输入新的APPID")->withInput();
        // $miniappDisable = Redis::get("miniapp_change_".$request->appid);
        // if (!empty($miniappDisable)) return back()->with("status","error")->with("msg","新切换的小程序正在切换中,请勿重复操作!")->withInput();
        Redis::del("miniapp_change_".$request->appid);
        Miniappswitch::dispatch($request->oldappid,$request->platfrom_type,$request->users_id,$request->appid)->delay(1);
        return redirect("/admin/setting/links_lists_public")->with("status","success")->with("msg","提交成功,10S后开始批量切换小程序链接！")->withInput();
    }

    public function redirectEngine(Request $request)
    {
        $users = Auth::user();
        if ($users->authority != 3) return redirect("/admin/links_lists/lists")->with("status","error")->with("msg","您无权修改链接！")->withInput();
        $redirectEngine = Redis::get("redirect_engine");
        return view("admin.index.redirect_engine",["redirectEngine"=>$redirectEngine]);
    }

    public function redirectEngineSubmit(Request $request)
    {
        Redis::set("redirect_engine",$request->redirect_engine);
        return redirect("/admin/setting/redirect_engine")->with("status","success")->with("msg","系统跳转方式修改成功!");
    }
}
